package com.lsy.weizhi2.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.WeiYouAdapter;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.mybase.MyTitleView;

/**
 * Created by 嗨哟 on 2017/3/13.
 */
public class WeiYouFragment extends Fragment {
    private Context context;
    private MyTitleView weiyou_title;
    private RecyclerView weiyou_rv;
    private int xx;
    private Handler handler = new Handler(Looper.getMainLooper());
    private int height;//title控件的高度，便于使用沉浸式效果
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.weiyou_fragment,container,false);
        weiyou_title= (MyTitleView) view.findViewById(R.id.weiyou_title);
        weiyou_rv= (RecyclerView) view.findViewById(R.id.weiyou_rv);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context=getActivity();
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        weiyou_rv.setLayoutManager(manager);
        WeiYouAdapter weiYouAdapter=new WeiYouAdapter(context,null);
        weiyou_rv.setAdapter(weiYouAdapter);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        xx=0;
        new Thread(new Runnable() {
            @Override
            public void run() {
                ViewTreeObserver vto = weiyou_title.getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (xx < 1) {
                            xx++;
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(weiyou_title.getLayoutParams());
                                    params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                        weiyou_title.setLayoutParams(params);
                                    }
                                }
                            });
                        }
                    }
                });
            }
        }).start();

    }
    @Override
    public void onPause() {
        super.onPause();
        xx=0;
    }
    @Override
    public void onStop() {
        super.onStop();
    }
}
